/**
 * Created by zhaoyuan on 14-2-17.
 */
Ext.onReady(function() {
    Ext.form.Field.prototype.msgTarget = 'qtip';
    Ext.QuickTips.init();
    var fm = Ext.form;
    var store2 = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: 'Get_User_List.action'
        }),
        reader: new Ext.data.JsonReader({
            fields: [{
                    name: 'NAME'
                }, {
                    name: 'PASSWORD'
                }, {
                    name: 'USERID'
                }, {
                    name: 'USERNAME'
                }]
        })
    });


    var toolbar = new Ext.Toolbar({
        id: 'toolbar',
        stateful: true,
        items: ['-', {
                id: 'add-p',
                text: '新增',
                handler: function() {
                    metadataForm.getForm().reset();
                    win.show();
                }

            }, '-', {
                id: 'edit-btn',
                text: '编辑',
                handler: function() {
                    var sel = grid.getSelectionModel().getSelected();
                    if (sel == null) {
                        Ext.Msg.alert("提示", "请勾选需要编辑的数据！");
                    } else {
                        metadataForm.getForm().loadRecord(sel);
                        win.show();
                    }

                }
            }, '-', {
                id: 'del-btn',
                text: '删除',
                handler: function() {
                    Ext.MessageBox.show({
                        title: '提示框',
                        msg: ' 确定删除该记录吗？注意,数据删除后将无法恢复！ ',
                        buttons: {
                            'yes': '确定',
                            'no': '取消'
                        },
                        fn: showResult,
                        icon: Ext.MessageBox.QUESTION
                    });

                    function showResult(btn) {
                        if (btn == 'yes') {
                            del();
                        }
                    }

                }
            }]
    });

    var form = new Ext.FormPanel({
        labelAlign: 'form',
        frame: true,
        bodyStyle: 'padding:5px 5px 0',
        width: '100%',
        items: [{
                layout: 'column',
                items: [
                    {
                        columnWidth: .3,
                        layout: 'form',
                        items: [
                            {
                                xtype: 'textfield',
                                fieldLabel: '姓名',
                                labelWidth: 20,
                                name: 'username',
                                id: 'username_form'
                            }
                        ]
                    }, {
                        columnWidth: .3,
                        layout: 'form',
                        items: [
                            {
                                xtype: 'textfield',
                                fieldLabel: '用户名',
                                name: 'name_form',
                                id: 'name_form'
                            }
                        ]
                    },
                    {
                        columnWidth: .1,
                        layout: 'form',
                        items: [{
                                xtype: "button",
                                id: 'query_tbar',
                                text: "查  询",
                                width: 60,
                                handler: function() {
                                    store2.load({
                                        params: {
                                            username: Ext.getCmp("username_form").getValue(),
                                            name: Ext.getCmp("name_form").getValue()
                                        }
                                    });
                                }
                            }]
                    }]
            }]
    });



    var sm = new Ext.grid.CheckboxSelectionModel({
    });

    var cm = new Ext.grid.ColumnModel([sm, {
            header: '编号',
            sortable: true,
            hidden: true,
            dataIndex: 'USERID'
        }, {
            header: '用户名',
            sortable: true,
            width: 50,
            dataIndex: 'NAME'
        }, {
            header: '姓名',
            sortable: true,
            dataIndex: 'USERNAME'
        }]);

    var grid = new Ext.grid.GridPanel({
        id: 'refuel_grid',
        store: store2,
        anchor: '100% -60',
        viewConfig: {
            forceFit: true
        },
        sm: sm,
        cm: cm,
        frame: true,
        columnLines: true,
        autoWidth: true,
        loadMask: true, // 数据加载时的遮罩功能
        stateful: true,
        stateId: 'grid'
    });

    var viewport = new Ext.Viewport({
        layout: 'border',
        items: [{
                region: 'center',
                layout: 'border',
                items: [{
                        tbar: toolbar,
                        region: 'center',
                        layout: "anchor",
                        frame: true,
                        deferredRender: false,
                        activeTab: 0,
                        items: [form, grid]
                    }]
            }]
    });

    function del() {
        var del_data = grid.getSelectionModel().getSelections();
        if (del_data.length == 0) {
            Ext.Msg.alert('提示', '请选择需要删除的数据！');
            return;
        } else {
            var id_array = [];
            for (var i = 0; i < del_data.length; i++) {
                id_array[i] = del_data[i].json.id;
            }
            sendRequest(id_array);
        }
    }

    function sendRequest(data) {
        Ext.Ajax.request({
            url: 'DeleteAction.action',
            params: {
                id: data,
                type: 'user'
            },
            success: function(response, options) {
                Ext.Msg.alert('提示', '删除成功！');
                store2.reload();
            },
            failure: function(response, options) {
                Ext.Msg.alert('提示', '删除失败！');
            }
        });
    }


    store2.load({
        params: {
            code: 'type'
        }
    });

    var metadataForm = new Ext.FormPanel({
        labelWidth: 75,
        url: '',
        frame: true,
        title: '',
        width: '100%',
        bodyStyle: 'padding:5px 5px 0',
        items: [{
                xtype: 'hidden',
                name: 'USERID'
            }, {
                xtype: 'textfield',
                fieldLabel: '用户名',
                name: 'NAME',
                allowBlank: false
            }, {
                xtype: 'textfield',
                fieldLabel: '姓名',
                name: 'USERNAME',
                allowBlank: false
            }, {
                xtype: 'textfield',
                fieldLabel: '密码',
                name: 'PASSWORD',
                allowBlank: false
            }]
    });

    var win = new Ext.Window({
        title: '编辑用户',
        closeAction: 'hide',
        width: 350,
        height: 250,
        buttonAlign: 'center',
        layout: 'fit',
        items: [metadataForm],
        buttons: [{
                text: '保存',
                handler: onSubmit
            }, {
                text: '关闭',
                handler: function() {
                    metadataForm.getForm().reset();
                    win.hide();

                }
            }]
    });




    function onSubmit() {
        // 提交表单
        if (metadataForm.getForm().isValid()) {
            var submitValues = metadataForm.getForm().getValues();
            for (var param in submitValues) {
                if (metadataForm.form.findField(param)
                        && metadataForm.form.findField(param).emptyText == submitValues[param]) {
                    metadataForm.form.findField(param).setValue(' ');
                }
            }
            metadataForm.getForm().submit({
                url: 'UserAction.action',
                waitMsg: '数据保存中...',
                submitEmptyText: false,
                success: function(c, d) {
                    Ext.MessageBox.alert("操作信息", "成功信息保存！");
                    metadataForm.getForm().reset();
                    store2.reload();
                },
                failure: function(c, d) {
                    Ext.MessageBox.show({
                        title: "操作信息",
                        msg: "信息保存出错，请联系管理员！",
                        buttons: Ext.MessageBox.OK,
                        icon: "ext-mb-error"
                    });
                }
            });
        }
    }
});